using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

namespace Test {
	class Ling {
		static void MainX() {
			Cache cac=new Cache("lin");
			MAL dal=new MAL();
			//dal.NonQry("truncate table dict");
			//dal.NonQry("create table dict(id int identity,word varchar(25),dict varchar(80),did int,wdef varchar(max))");
			//dal.NonQry("truncate table dict01");
			dal.NonQry("drop table dict01");
			dal.NonQry("create table dict01(id int auto_increment primary key,word varchar(55),dict varchar(80),did int,wdef text)character set utf8 collate utf8_polish_ci");
			//foreach(string wrd in File.ReadAllLines("wordsEn.txt"))
			while(true)
			{
				DataTable tab=dal.Query("select id,word,link from dictWrd where lastchecked is null limit 50");

				foreach(DataRow row in tab.Rows)
				{
					string wrd=row["word"].ToString();
					string link=row["link"].ToString();
					string id=row["id"].ToString();

					string cont=cac.Get("http://ling.pl/"+link,5000);
					if(string.IsNullOrEmpty(cont))
					{
						dal.NonQry("update dictWrd set lastchecked=now(), status='N'where id={0}",id);
						continue;
					}
					string[] langs=Nod.Dbls(cont,"<div id=\"lang-1");
					foreach(string lang in langs)
					{
						string[] dname=Nod.Dbls(lang,"<div class=\"dict-(\\d+?) dictRealName");
						string[] trans=Nod.Dbls(lang,"<div class=\"grid_12 alpha translate omega\">");
						System.Diagnostics.Debug.Assert(dname.Length==trans.Length);

						for(int i=0;i<dname.Length;i++)
						{
							string dictId=Nod.Rx(dname[i],"<div class=\"dict-(\\d+?) dictRealName");
							string dictName=System.Web.HttpUtility.HtmlDecode(Nod.Inr(dname[i],"<div class=\"dict-"));

							string[] AllDefs=Nod.Dbls(trans[i],"<p class=\"dictDef\">");
							List<string> _allDefs=new List<string>();
							foreach(string AllDef in AllDefs)
							{


								string dictTrans=Nod.Inr(AllDef,"<p class=\"dictDef\">");

								string dictTransFianl=System.Web.HttpUtility.HtmlDecode(dictTrans);
								//dictTransFianl=Encoding.UTF8.GetString(Encoding.Default.GetBytes(dictTransFianl));

								while(dictTransFianl.EndsWith("<br />")||dictTransFianl.EndsWith("<br/>")||dictTransFianl.EndsWith("</br>")||dictTransFianl.EndsWith("<BR>")||dictTransFianl.EndsWith("</BR>"))
									dictTransFianl=dictTransFianl.Substring(0,dictTransFianl.LastIndexOf('<'));

								if(dictTransFianl!=""&&!_allDefs.Contains(dictTransFianl))
									_allDefs.Add(dictTransFianl);
							}
							string Translation=string.Join(", ",_allDefs.ToArray());

							if(dictId=="51"||dictId=="50")
								Translation=Translation.ToLower();
							//dal.NonQry("create table dict(id int auto_increment primary key,word varchar(25),dict varchar(80),did int,wdef text)");
							if(wrd.Contains("("))
								wrd=System.Text.RegularExpressions.Regex.Replace(wrd," \\(.+?\\)","").Trim();
							if(wrd.Length>55)
							{
								dal.NonQry("update dictWrd set lastchecked=now(), status='L'where id={0}",id);
								continue;
							}

							dal.NonQry("insert into dict01(word,dict,did,wdef)values('{0}','{1}',{2},'{3}')",wrd.Replace("'","''"),dictName.Replace("'","''"),dictId,Translation.Replace("\\","").Replace("'","''"));
							dal.NonQry("update dictWrd set lastchecked=now(), status='E'where id={0}",id);
						}


					}
				}
				//string dicts=Nod.Dbl(cont,"<div class=\"definition\">");
				//string[] divs=Nod.Dbls(defs,"<div id='dictdiv");
				//foreach(string def in divs)
				//{
				//  string dId=Nod.Rx(def,"<div id='dictdiv_(\\d+?)'>");
				//  string dict=Nod.Rx(def,"<div class='dictname'>(.+?)</div>");
				//  string dwrd=Nod.InnerText(Nod.Rx(def,"<div class='dictword'>(.+?)</div>"));
				//  string wdef=Nod.Rx(def,"<div class='dictdef'>(.+?)</div>").Trim();

				//  dal.NonQry("insert into dict(word,dict,did,wdef)values('{0}','{1}',{2},'{3}')",dwrd,dict.Replace("'","''"),dId,wdef.Replace("'","''"));

				//}
			}


		}
	}
}
